package cn.com.cifi.mars.web.action.sys;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.bean.dto.SysProcessConfigDto;
import cn.com.cifi.mars.bean.vo.sys.req.DeleteSysProcessConfigReqParam;
import cn.com.cifi.mars.bean.vo.sys.req.SaveSysProcessConfigReqParam;
import cn.com.cifi.mars.bean.vo.sys.req.UpdateSysProcessConfigReqParam;
import cn.com.cifi.mars.bean.vo.sys.rsp.OrgTreeVo;
import cn.com.cifi.mars.bean.vo.sys.rsp.SysProcessConfigVo;
import cn.com.cifi.mars.common.base.BaseAction;
import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.constant.LogEventEnum;
import cn.com.cifi.mars.constant.LogSrcEnum;
import cn.com.cifi.mars.entity.common.IdmUser;
import cn.com.cifi.mars.service.OrgService;
import cn.com.cifi.mars.service.SysLogService;
import cn.com.cifi.mars.service.SysProcessConfigService;
import cn.com.cifi.mars.util.Exportutils;
import cn.com.cifi.mars.util.WebUtils;
import cn.com.cifi.mars.web.action.masterDataToMars.MasterDataToMarsController;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyd
 * @since 2019-09-21
 */
@Slf4j
@RestController
public class SysProcessConfigController extends BaseAction implements ISysProcessConfigController{

	@Autowired
	private SysProcessConfigService sysProcessConfigService;
	@Autowired
	private SysLogService sysLogService;
	@Autowired
	private OrgService orgService;
	
	/**
	 * 添加流程配置
	 */
	@Override
	public R<String> createSysProcessConfig(String apiVersion, SaveSysProcessConfigReqParam param) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		IdmUser user = getOperator();
		// 新增日志
		SysLogBO bo = new SysLogBO();
		bo.setEvent(LogEventEnum.ADD);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("系统配置");
		bo.setTitle("添加流程配置");
		bo.setContent("添加流程配置");
		boolean msg = sysLogService.add(bo, user.getAlias());
		return ok(sysProcessConfigService.add(param, user.getAlias()));
	
	}

	/**
	 * 编辑流程配置
	 */
	@Override
	public R<String> updateSysProcessConfig(String apiVersion, UpdateSysProcessConfigReqParam param) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		IdmUser user = getOperator();
		// 新增日志
		SysLogBO bo = new SysLogBO();
		bo.setEvent(LogEventEnum.ADD);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("系统配置");
		bo.setTitle("编辑流程配置");
		bo.setContent("编辑流程配置");
		boolean msg = sysLogService.add(bo, user.getAlias());
		return ok(sysProcessConfigService.update(param, user.getAlias()));
	
	}

	

	/**
	 * 删除流程配置
	 */
	@Override
	public R<String> deleteSysProcessConfigById(String apiVersion, DeleteSysProcessConfigReqParam param) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		IdmUser user = getOperator();
		// 新增日志
		SysLogBO bo = new SysLogBO();
		bo.setEvent(LogEventEnum.DELETE);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("系统配置");
		bo.setTitle("删除流程配置");
		bo.setContent("删除流程配置");
		boolean msg = sysLogService.add(bo, user.getAlias());
		return ok(sysProcessConfigService.delete(param.getId()));
	}
	
	/**
	 * 流程配置分页
	 */
	@Override
	public R<SysProcessConfigVo> getSysProcessConfigPage(String apiVersion, String current, String pageSize,String regionId) {
		  return ok(sysProcessConfigService.querySysProcessConfigPage(current,pageSize,regionId));
	}

	/**
	 * 导出流程配置
	 * @throws Exception 
	 */
	@Override
	public void export(String apiVersion,HttpServletResponse response,String regionId){
	    HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("流程配置表");
        createTitle(workbook,sheet);
        List<SysProcessConfigDto>list=sysProcessConfigService.queryProcessConfigList(regionId);
        //设置日期格式
        HSSFCellStyle style = workbook.createCellStyle();
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
        //新增数据行，并且设置单元格数据
        int rowNum=1;
        for(SysProcessConfigDto sys:list){
            HSSFRow row = sheet.createRow(rowNum);
            row.createCell(0).setCellValue(sys.getTypeName());
            row.createCell(1).setCellValue(sys.getKeyId());
            row.createCell(2).setCellValue(sys.getProDesc());
            row.createCell(3).setCellValue(sys.getCreateby());
            row.createCell(4).setCellValue(sys.getCreateStr());
            row.createCell(5).setCellValue(sys.getTypeId());
            row.createCell(6).setCellValue(sys.getRegionId());
            row.createCell(7).setCellValue(sys.getRegionName());
            HSSFCell cell = row.createCell(8);
           // cell.setCellValue(user.getCreate_time());
            cell.setCellStyle(style);
            rowNum++;
        }
        String fileName = "导出流程配置.xls";
        //生成excel文件
        try {
        	Exportutils.buildExcelFile(fileName, workbook);
		} catch (Exception e) {
			log.error("###### errorMsg: ", e);
		}
        //浏览器下载excel
        try {
			Exportutils.buildExcelDocument(fileName,workbook,response);
		} catch (Exception e) {
			log.error("###### errorMsg: ", e);
		}
	}
	
	//创建表头
    private void createTitle(HSSFWorkbook workbook,HSSFSheet sheet){
        HSSFRow row = sheet.createRow(0);
        //设置列宽，setColumnWidth的第二个参数要乘以256，这个参数的单位是1/256个字符宽度
        sheet.setColumnWidth(1,13*256);
        sheet.setColumnWidth(2,12*256);
        sheet.setColumnWidth(3,30*256);
        sheet.setColumnWidth(4,29*256);
        sheet.setColumnWidth(5,30*256);
        sheet.setColumnWidth(6,30*256);
        sheet.setColumnWidth(7,30*256);
        
        //设置为居中加粗
        HSSFCellStyle style = workbook.createCellStyle();
        HSSFFont font = workbook.createFont();
        font.setBold(true);
        HorizontalAlignment align=HorizontalAlignment.CENTER;
        style.setAlignment(align);
        style.setFont(font);
        HSSFCell cell;
        cell = row.createCell(0);
        cell.setCellValue("类型名称");
        cell.setCellStyle(style);

        cell = row.createCell(1);
        cell.setCellValue("流程key");
        cell.setCellStyle(style);

        cell = row.createCell(2);
        cell.setCellValue("流程说明");
        cell.setCellStyle(style);

        cell = row.createCell(3);
        cell.setCellValue("创建人");
        cell.setCellStyle(style);
        
        cell = row.createCell(4);
        cell.setCellValue("创建日期");
        cell.setCellStyle(style);
        
        cell = row.createCell(5);
        cell.setCellValue("类型id");
        cell.setCellStyle(style);
        
        cell = row.createCell(6);
        cell.setCellValue("区域Id");
        cell.setCellStyle(style);
        
        cell = row.createCell(7);
        cell.setCellValue("区域名称");
        cell.setCellStyle(style);
    }

    /**
     * 导入流程配置
     */
	@Override
	public R<String> importSysProcessConfig(String apiVersion, MultipartFile file) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		IdmUser user = getOperator();
		SysLogBO bo = new SysLogBO();
		bo.setEvent(LogEventEnum.ADD);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("流程配置");
		bo.setTitle("导入流程配置");
		bo.setContent("导入流程配置");
		boolean msg = sysLogService.add(bo, user.getAlias());
		return ok(sysProcessConfigService.importSysProcessConfig(file,user.getAlias()));
	}
    
	
	/**
	 * 流程配置左侧组织机构
	 */
	@Override
	public R<List<OrgTreeVo>> getOrgTree(String apiVersion) {
		 return ok(orgService.queryProcessConfigTree());
	}
	
	/**
	 * 流程配置编辑时查询区域层级
	 */
	@Override
	public R<String> getRank(String apiVersion, String id) {
		return ok(sysProcessConfigService.getRank(id));
	}
}
